package com.itheima.chapter08.set.TreeSet;

import java.util.Comparator;
import java.util.TreeSet;
//Comparator比较器排序
public class Comparatordemo {
    public static void main(String[] args) {
        TreeSet<Teacher> ts =new TreeSet<>(new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
                int result = o1.getAge() - o2.getAge();//主要条件
                //o1表示要存入的数
                //o2表示已经存入的数
                result= result==0? o1.getName().compareTo(o2.getName()):result;
                //次要条件姓名排序
                return result;
            }
        });
        System.out.println("-----------------");
        Teacher t1 =new Teacher("zhangsan",25);
        Teacher t2 =new Teacher("lisi",36);
        Teacher t3 =new Teacher("zhaoliu",25);
        Teacher t4 =new Teacher("liuji",22);
        ts.add(t1);
        ts.add(t2);
        ts.add(t3);
        ts.add(t4);
        System.out.println(ts);

    }
}
